

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-blog/img/fluid.png">
  <link rel="icon" href="/my-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="木舟">
  <meta name="keywords" content="python Vue Test 测试开发 自动化测试">
  
    <meta name="description" content="注释单行注释以#开头，#右边的所有东西当做说明，而不是真正要执行的程序，起辅助说明作用 如果我们使用ide,也就是代码编辑器来进行编写代码的话. 可以通过 ctrl + &#x2F; 来快速注释 12# 我是注释，可以在里写一些功能说明之类print(&amp;#x27;hello world&amp;#x27;)  多行注释以’’’XXX’’’包裹，或以”””XXX”””包裹 12345678910&amp;#x27;&amp;#x27">
<meta property="og:type" content="article">
<meta property="og:title" content="python基础（上）">
<meta property="og:url" content="https://walker_kwu.gitee.io/my-blog/2023/02/19/python/base/python%E5%9F%BA%E7%A1%80%EF%BC%88%E4%B8%8A%EF%BC%89/">
<meta property="og:site_name" content="My Blog">
<meta property="og:description" content="注释单行注释以#开头，#右边的所有东西当做说明，而不是真正要执行的程序，起辅助说明作用 如果我们使用ide,也就是代码编辑器来进行编写代码的话. 可以通过 ctrl + &#x2F; 来快速注释 12# 我是注释，可以在里写一些功能说明之类print(&amp;#x27;hello world&amp;#x27;)  多行注释以’’’XXX’’’包裹，或以”””XXX”””包裹 12345678910&amp;#x27;&amp;#x27">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-02-19T06:30:08.000Z">
<meta property="article:modified_time" content="2023-05-23T10:37:53.265Z">
<meta property="article:author" content="木舟">
<meta property="article:tag" content="测试开发">
<meta property="article:tag" content="Python基础">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>python基础（上） - My Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"walker_kwu.gitee.io","root":"/my-blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-blog/js/utils.js" ></script>
  <script  src="/my-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-blog/">
      <strong>木舟</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="python基础（上）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        木舟
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-02-19 14:30" pubdate>
          2023年2月19日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.1k 字
        
      </span>
    

    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">python基础（上）</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月23日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h1><h2 id="单行注释"><a href="#单行注释" class="headerlink" title="单行注释"></a>单行注释</h2><p>以#开头，#右边的所有东西当做说明，而不是真正要执行的程序，起辅助说明作用</p>
<p>如果我们使用ide,也就是代码编辑器来进行编写代码的话. 可以通过 <code>ctrl + /</code> 来快速注释</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 我是注释，可以在里写一些功能说明之类</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;hello world&#x27;</span>)<br></code></pre></td></tr></table></figure>

<h2 id="多行注释"><a href="#多行注释" class="headerlink" title="多行注释"></a>多行注释</h2><p>以’’’XXX’’’包裹，或以”””XXX”””包裹</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-string">    我是多行注释，可以写很多很多行的功能说明</span><br><span class="hljs-string">    我是多行注释，可以写很多很多行的功能说明</span><br><span class="hljs-string">    我是多行注释，可以写很多很多行的功能说明</span><br><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">    我也是多行注释</span><br><span class="hljs-string">    我也是多行注释</span><br><span class="hljs-string">    我也是多行注释</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>


<h1 id="输入与输出"><a href="#输入与输出" class="headerlink" title="输入与输出"></a>输入与输出</h1><h2 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h2><p>就是我们上面用到 <code>print</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 基本使用，输出会换行，因为 end参数默认为 \n</span><br><span class="hljs-built_in">print</span>(<span class="hljs-number">1</span>)<br><br><span class="hljs-comment"># 多个参数</span><br><span class="hljs-built_in">print</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)<br><br><span class="hljs-comment"># 定义结束符</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;a&#x27;</span>, end=<span class="hljs-string">&#x27;1&#x27;</span>)<br><br><span class="hljs-comment"># 输出不换行</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;a&#x27;</span>, end=<span class="hljs-string">&#x27;&#x27;</span>)<br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;a&#x27;</span>, end=<span class="hljs-string">&#x27;&#x27;</span>)<br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果：</span><br><span class="hljs-string">1</span><br><span class="hljs-string">1 2</span><br><span class="hljs-string">a1aa</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<h2 id="输入"><a href="#输入" class="headerlink" title="输入"></a>输入</h2><p><code>input()</code> 函数可以获取用户输入的内容，获取的所有内容都是字符串类型</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># input: 出来的全部都是字符串类型。</span><br>username = <span class="hljs-built_in">input</span>(<span class="hljs-string">&#x27;请输入用户名：&#x27;</span>)<br>password = <span class="hljs-built_in">input</span>(<span class="hljs-string">&#x27;请输入密码：&#x27;</span>)<br><span class="hljs-built_in">print</span>(username, <span class="hljs-built_in">type</span>(username))<br><span class="hljs-built_in">print</span>(password, <span class="hljs-built_in">type</span>(password))<br></code></pre></td></tr></table></figure>


<h1 id="变量及数据类型"><a href="#变量及数据类型" class="headerlink" title="变量及数据类型"></a>变量及数据类型</h1><h2 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h2><p>在程序中，有时我们需要对2个数据进行求和，那么该怎样做呢？<br>大家类比一下现实生活中，比如去超市买东西，往往咱们需要一个菜篮子，用来进行存储物品，等到所<br>有的物品都购买完成后，在收银台进行结账即可<br>如果在程序中，需要把2个数据，或者多个数据进行求和的话，那么就需要把这些数据先存储起来，然<br>后把它们累加起来即可<br>在Python中，存储一个数据，需要一个叫做变量的东西，如下示例:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python">num1 = <span class="hljs-number">100</span> <span class="hljs-comment"># num1就是一个变量，就好一个小菜篮子</span><br>num2 = <span class="hljs-number">87</span>  <span class="hljs-comment"># num2也是一个变量</span><br>result = num1 + num2 <span class="hljs-comment"># 把num1和num2这两个&quot;菜篮子&quot;中的数据进行累加，然后放到 result变量中</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">所谓变量，可以理解为菜篮子，如果需要存储多个数据，最简单的方式是有多个变量，当然</span><br><span class="hljs-string">了也可以使用一个</span><br><span class="hljs-string">程序就是用来处理数据的，而变量就是用来存储数据的</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<h2 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h2><h3 id="生活中的类型"><a href="#生活中的类型" class="headerlink" title="生活中的类型"></a>生活中的类型</h3><p>把程序中的数据类型比作生活中的物品类型, 就好像超市中的食物、工具、容器</p>
<p>超市中每种类型的商品都有各自类型的作用，那么程序中也是。</p>
<p>变量也可以分为多种数据类型，每种数据类型起着不同的作用。</p>
<h3 id="python数据类型"><a href="#python数据类型" class="headerlink" title="python数据类型"></a>python数据类型</h3><p>后续我们会根据以下数据类型进行学习：</p>
<ul>
<li>Number，Number又分为两小类<ul>
<li>int，整型，也就是整数</li>
<li>float，浮点型，也就是小数</li>
</ul>
</li>
<li>Bool，布尔值，要么为真要么为假</li>
<li>Str，字符串</li>
<li>List，列表</li>
<li>Tuple，元组</li>
<li>Dict，字典</li>
<li>Set, 集合</li>
</ul>
<p>怎么查看一个变量的数据类型</p>
<ul>
<li>type</li>
<li>isinstance</li>
</ul>
<h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><p>python是强类型语言，一个变量的类型，一旦声明，如果不进行重新赋值，则类型不会改变。</p>
<h2 id="命名规则"><a href="#命名规则" class="headerlink" title="命名规则"></a>命名规则</h2><ul>
<li>顾名思义，起名时需要根据业务场景、技术用途进行命名，比如：项目列表 -&gt; projectList</li>
<li>命名格式<ul>
<li>驼峰命名，第一个单词首字母小写，后续单词首字母大写：relateProjectList</li>
<li>蛇形命名，单词全部小写，单词之间使用 <code>_</code> 隔开，relate_project_list</li>
<li>大驼峰式命名法，全部单词的首字母都大写，一般编程时只会用在定义 <strong>常量</strong>，即 <strong>具有特殊意义且其值不会被修改的变量</strong></li>
</ul>
</li>
</ul>
<h1 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h1><h2 id="算数运算符"><a href="#算数运算符" class="headerlink" title="算数运算符"></a>算数运算符</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">10</span><br>b = <span class="hljs-number">20</span><br><span class="hljs-built_in">print</span>(a + b)<br><span class="hljs-built_in">print</span>(a - b)<br><span class="hljs-built_in">print</span>(a * b)<br><span class="hljs-built_in">print</span>(a / b)<br><span class="hljs-built_in">print</span>(a % b)    <span class="hljs-comment"># 取余</span><br><span class="hljs-built_in">print</span>(a // b)   <span class="hljs-comment"># 取整</span><br><span class="hljs-built_in">print</span>(a ** b)   <span class="hljs-comment"># 幂等</span><br></code></pre></td></tr></table></figure>

<h2 id="比较运算"><a href="#比较运算" class="headerlink" title="比较运算"></a>比较运算</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-built_in">print</span>(a &gt; b)<br><span class="hljs-built_in">print</span>(a &lt; b)<br><span class="hljs-built_in">print</span>(a == b)   <span class="hljs-comment"># 比较的是两边的值是否相等</span><br><span class="hljs-built_in">print</span>(a != b)<br><span class="hljs-built_in">print</span>(a &gt;= b)<br><span class="hljs-built_in">print</span>(a &lt;= b)<br></code></pre></td></tr></table></figure>

<h2 id="赋值运算符"><a href="#赋值运算符" class="headerlink" title="赋值运算符"></a>赋值运算符</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs python">a = b<br><span class="hljs-built_in">print</span>(a)<br>a += b<br><span class="hljs-built_in">print</span>(a)<br>a -= b<br><span class="hljs-built_in">print</span>(a)<br>a *= b<br><span class="hljs-built_in">print</span>(a)<br>a /= b<br><span class="hljs-built_in">print</span>(a)<br>a %= b<br><span class="hljs-built_in">print</span>(a)<br>a //= b<br><span class="hljs-built_in">print</span>(a)<br>a **= b<br><span class="hljs-built_in">print</span>(a)<br></code></pre></td></tr></table></figure>

<h2 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h2><p>and or not</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-built_in">print</span>(<span class="hljs-number">10</span> &gt; <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> <span class="hljs-number">20</span> &lt; <span class="hljs-number">2</span>)<br><span class="hljs-built_in">print</span>(<span class="hljs-number">10</span> &gt; <span class="hljs-number">2</span> <span class="hljs-keyword">or</span> <span class="hljs-number">20</span> &lt; <span class="hljs-number">2</span>)<br><span class="hljs-built_in">print</span>(<span class="hljs-keyword">not</span> <span class="hljs-number">10</span> &gt; <span class="hljs-number">2</span>)<br></code></pre></td></tr></table></figure>

<h2 id="身份运算"><a href="#身份运算" class="headerlink" title="身份运算"></a>身份运算</h2><p>is</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-literal">None</span><br><span class="hljs-built_in">print</span>(a <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>)    <span class="hljs-comment"># 判断的是内存地址是否相同</span><br><span class="hljs-built_in">print</span>(a <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">True</span>)<br><br>i = <span class="hljs-number">1</span><br>p = <span class="hljs-number">1</span><br><span class="hljs-built_in">print</span>(i <span class="hljs-keyword">is</span> p)<br><br></code></pre></td></tr></table></figure>


<h1 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h1><p>定义：Python中的字符串用单引号/双引号/三个单/双引号括起来</p>
<blockquote>
<p>任何数据类型以及数据结构都可以通过str()进行转化成字符串</p>
</blockquote>
<h2 id="创建方式"><a href="#创建方式" class="headerlink" title="创建方式"></a>创建方式</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 一对单、双引号创建</span><br><span class="hljs-comment"># python有一种建议规范：单引号一般用作非中文、双引号一般用作中文</span><br>i = <span class="hljs-string">&#x27;str&#x27;</span><br>i = <span class="hljs-string">&quot;str&quot;</span><br><br><span class="hljs-comment"># 三对单、双引号创建</span><br><span class="hljs-comment"># 区别于【一对】, 这种创建方式使输出的内容换行</span><br><br><span class="hljs-comment"># 不换行</span><br>i = <span class="hljs-string">&#x27;&#x27;&#x27;str&#x27;&#x27;&#x27;</span><br>i = <span class="hljs-string">&quot;&quot;&quot;str&quot;&quot;&quot;</span><br><br><span class="hljs-comment"># 可换行</span><br>i = <span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">str</span><br><span class="hljs-string">str</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><br>i = <span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-string">str</span><br><span class="hljs-string">str</span><br><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-comment"># 单双引号可以配合使用</span><br>content = <span class="hljs-string">&#x27;I am taibai, 18 year old&#x27;</span><br>content = <span class="hljs-string">&quot;I&#x27;m  &#x27;taibai&#x27; , 18 year old&quot;</span><br><br></code></pre></td></tr></table></figure>

<h2 id="拼接-复制-转换"><a href="#拼接-复制-转换" class="headerlink" title="拼接/复制/转换"></a>拼接/复制/转换</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 字符串是可以通过 + 进行拼接的，前提是拼接两者必须都是字符串</span><br>m = <span class="hljs-string">&quot;木&quot;</span><br>z = <span class="hljs-string">&quot;舟&quot;</span><br><span class="hljs-built_in">print</span>(m + z)  <span class="hljs-comment"># 木舟</span><br><br><br><span class="hljs-comment"># 如果字符串的值为数字，可以通过int函数进行转化成整型，浮点型则可以通过float</span><br><span class="hljs-comment"># type函数可以用来查看变量的数据类型</span><br>i = <span class="hljs-string">&#x27;1&#x27;</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">int</span>(i)) <span class="hljs-comment"># 输出为整型的1</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">type</span>(<span class="hljs-built_in">int</span>(i))) <span class="hljs-comment"># class:int</span><br><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;a&#x27;</span> * <span class="hljs-number">10</span>)<br></code></pre></td></tr></table></figure>

<h2 id="切片取值"><a href="#切片取值" class="headerlink" title="切片取值"></a>切片取值</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs python">i = <span class="hljs-string">&#x27;muzho&#x27;</span><br><span class="hljs-built_in">print</span>(i[<span class="hljs-number">0</span>]) <span class="hljs-comment"># m</span><br><br><span class="hljs-comment"># 切片, 遵循准则：左取右不拿</span><br><span class="hljs-built_in">print</span>(i[<span class="hljs-number">0</span>:<span class="hljs-number">2</span>]) <span class="hljs-comment"># mu</span><br><span class="hljs-built_in">print</span>(i[<span class="hljs-number">2</span>:<span class="hljs-number">4</span>]) <span class="hljs-comment"># zh</span><br><span class="hljs-built_in">print</span>(i[<span class="hljs-number">1</span>:]) <span class="hljs-comment"># uzho</span><br><span class="hljs-built_in">print</span>(i[:<span class="hljs-number">3</span>]) <span class="hljs-comment"># muz</span><br><span class="hljs-built_in">print</span>(i[:-<span class="hljs-number">2</span>]) <span class="hljs-comment"># 这里的-2对应倒数第二位，根据左取右不拿原则，muz</span><br><br><br><span class="hljs-comment"># 切片-步长</span><br><span class="hljs-comment"># 切片可接收三个参数，以冒号:作为分割，第三个参数为步长,默认为1，可取负数</span><br><span class="hljs-built_in">print</span>(i[<span class="hljs-number">0</span>:<span class="hljs-number">5</span>:<span class="hljs-number">2</span>]) <span class="hljs-comment"># 从第一个元素开始，每两元素取一次，mzo</span><br><br><span class="hljs-comment"># 步长负数，一般用作反转，此方法对数组同样有效</span><br><span class="hljs-built_in">print</span>(i[::-<span class="hljs-number">1</span>]) <span class="hljs-comment"># 负数意思就是倒着走，ohzum</span><br></code></pre></td></tr></table></figure>

<h2 id="转义符"><a href="#转义符" class="headerlink" title="转义符"></a>转义符</h2><p>比较常用的就是 <code>\n</code>, <code>\t</code>, 分别代表 <strong>回车</strong> <strong>制表符 也就是一个tab</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs python">str4=<span class="hljs-string">&#x27;你好\n我叫\t张三，今年18一枝花&#x27;</span><br><span class="hljs-built_in">print</span>(str4)<br></code></pre></td></tr></table></figure>

<p>那么我们怎么正常的输出 <strong>转义符</strong> 呢?</p>
<p>需要在转义符前面再加一个 <code>\</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\\n&quot;</span>)<br></code></pre></td></tr></table></figure>

<h2 id="格式化输出"><a href="#格式化输出" class="headerlink" title="格式化输出"></a>格式化输出</h2><h3 id="f-string"><a href="#f-string" class="headerlink" title="f-string"></a>f-string</h3><p>在字符串前加前缀  <code>f</code> ，通过  <code>&#123;expression&#125;</code>  表达式，把 Python 表达式的值添加到字符串内</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs python">name=<span class="hljs-string">&#x27;张三&#x27;</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">f&#x27;你好，我叫<span class="hljs-subst">&#123;name&#125;</span>&#x27;</span>)<br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果:</span><br><span class="hljs-string">你好, 我叫张三</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 格式说明符是可选的，写在表达式后面，可以更好地控制格式化值的方式。</span><br>i = <span class="hljs-number">1.123</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;aaa<span class="hljs-subst">&#123;i:<span class="hljs-number">.2</span>f&#125;</span>&quot;</span>)  <span class="hljs-comment"># 保留两位小数</span><br></code></pre></td></tr></table></figure>

<h3 id="占位符输出"><a href="#占位符输出" class="headerlink" title="占位符输出"></a>占位符输出</h3><p><code>%</code> 运算符（求余符）也可用于字符串格式化。给定  ‘string’ % values ，则  string  中的  %  实例会<br>以零个或多个  values  元素替换。此操作被称为字符串插值。例如：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 整数</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;%d&#x27;</span> % <span class="hljs-number">20</span>)<br><span class="hljs-comment"># 浮点数</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;%.1f&#x27;</span> % <span class="hljs-number">1.11</span>)  <span class="hljs-comment"># 取1位小数</span><br><span class="hljs-comment"># 字符串</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;%s&#x27;</span> % <span class="hljs-string">&#x27;hello world&#x27;</span>)<br><br>name = <span class="hljs-string">&quot;张三&quot;</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;我叫 %s&quot;</span> % name)<br></code></pre></td></tr></table></figure>

<h3 id="format用法"><a href="#format用法" class="headerlink" title="format用法"></a>format用法</h3><p>相对基本格式化输出采用 ‘%’ 的方法，format()功能更强大，该函数把字符串当成一个模板，通过传入的<br>参数进行格式化，并且使用大括号‘{}’作为特殊字符代替‘%’</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;&#123;&#125; &#123;&#125;&#x27;</span>.<span class="hljs-built_in">format</span>(<span class="hljs-string">&#x27;hello&#x27;</span>,<span class="hljs-string">&#x27;world&#x27;</span>))  <span class="hljs-comment"># 不带字段</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;&#123;0&#125; &#123;1&#125;&#x27;</span>.<span class="hljs-built_in">format</span>(<span class="hljs-string">&#x27;hello&#x27;</span>,<span class="hljs-string">&#x27;world&#x27;</span>))  <span class="hljs-comment"># 带数字编号</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;&#123;0&#125; &#123;1&#125; &#123;0&#125;&#x27;</span>.<span class="hljs-built_in">format</span>(<span class="hljs-string">&#x27;hello&#x27;</span>,<span class="hljs-string">&#x27;world&#x27;</span>))  <span class="hljs-comment"># 打乱顺序</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;&#123;a&#125; &#123;tom&#125; &#123;a&#125;&#x27;</span>.<span class="hljs-built_in">format</span>(tom=<span class="hljs-string">&#x27;hello&#x27;</span>,a=<span class="hljs-string">&#x27;world&#x27;</span>))  <span class="hljs-comment"># 带关键字</span><br></code></pre></td></tr></table></figure>


<h2 id="常见函数"><a href="#常见函数" class="headerlink" title="常见函数"></a>常见函数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs python">str13=<span class="hljs-string">&quot;abcdef123_&quot;</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;转化为大写：&quot;</span>,str13.upper())   <span class="hljs-comment"># upper 转化为大写</span><br><span class="hljs-built_in">print</span>(str13.lower())    <span class="hljs-comment"># casefold/lower 字符串全变小写</span><br><span class="hljs-built_in">print</span>(str13.capitalize())   <span class="hljs-comment"># capitalize 首字母大写</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;是否字母或者数字:&quot;</span>,str13.isalnum())  <span class="hljs-comment"># isalnum 是否字母或者数字 ，返回True  </span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;是否是字母或者中文:&quot;</span>,str13.isalpha())     <span class="hljs-comment"># isalpha  是否是字母或者中文</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;是否为纯数字&quot;</span>,str13.isnumeric())   <span class="hljs-comment"># isdigit/isnumeric 是否为纯数字</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">max</span>(str13))   <span class="hljs-comment"># 取最大值，是通过ASCII来进行比较</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">min</span>(str13))   <span class="hljs-comment"># 取最小值，是通过ASCII来进行比较</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">len</span>(str13))   <span class="hljs-comment"># 计算字符串长度</span><br><br>names=<span class="hljs-string">&quot;jeffery NN water&quot;</span><br><span class="hljs-built_in">print</span>(names.find(<span class="hljs-string">&quot;NN&quot;</span>))    <span class="hljs-comment"># 找到字符返回匹配字符的开始索引值，否则返回-1</span><br><span class="hljs-built_in">print</span>(names.find(<span class="hljs-string">&quot;NN&quot;</span>,<span class="hljs-number">0</span>,<span class="hljs-built_in">len</span>(names)))    <span class="hljs-comment"># 找到字符返回匹配字符的开始索引值，否则返回-1</span><br><span class="hljs-built_in">print</span>(names.index(<span class="hljs-string">&quot;NN&quot;</span>))   <span class="hljs-comment"># 找到字符返回匹配字符的开始索引值，没有找到报异常</span><br><span class="hljs-built_in">print</span>(names.index(<span class="hljs-string">&quot;NN&quot;</span>,<span class="hljs-number">0</span>,<span class="hljs-built_in">len</span>(names)))   <span class="hljs-comment"># 找到字符返回匹配字符的开始索引值，没有找到报异常</span><br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;-&#x27;</span>.join(names))  <span class="hljs-comment"># 按照指定分隔符连接字符串各个元素,列表也行  join</span><br><span class="hljs-built_in">print</span>(names.replace(<span class="hljs-string">&quot;NN&quot;</span>,<span class="hljs-string">&quot;MS221&quot;</span>))   <span class="hljs-comment"># 替换字符串中部分字符  replace</span><br><span class="hljs-built_in">print</span>(names.replace(<span class="hljs-string">&quot;NN&quot;</span>,<span class="hljs-string">&quot;MS221&quot;</span>, <span class="hljs-number">1</span>))   <span class="hljs-comment"># 替换字符串中部分字符, 可以指定替换次数</span><br><br>newstr=<span class="hljs-string">&#x27;h-e-l-l-o-w-o-r-l-d&#x27;</span><br><span class="hljs-built_in">print</span>(newstr.split(<span class="hljs-string">&#x27;-&#x27;</span>))    <span class="hljs-comment"># 根据指定的分隔符把字符串转为List  split</span><br><span class="hljs-built_in">print</span>(newstr.split(<span class="hljs-string">&#x27;-&#x27;</span>, <span class="hljs-number">1</span>))    <span class="hljs-comment"># 根据指定的分隔符把字符串转为List, 可以指定分割次数</span><br><br>str18=<span class="hljs-string">&quot; abcdefg &quot;</span><br><span class="hljs-built_in">print</span>(str18.strip())    <span class="hljs-comment"># 前后去空格 strip()</span><br><span class="hljs-built_in">print</span>(str18.rstrip())   <span class="hljs-comment"># rstrip()右边去空格</span><br><span class="hljs-built_in">print</span>(str18.lstrip())   <span class="hljs-comment"># lstrip()左边去空格</span><br></code></pre></td></tr></table></figure>

<h2 id="其他字符串函数（了解）"><a href="#其他字符串函数（了解）" class="headerlink" title="其他字符串函数（了解）"></a>其他字符串函数（了解）</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-string">center指定长度居中对齐且空格处指定字符填充</span><br><span class="hljs-string">count统计指定字符个数</span><br><span class="hljs-string">encode编码</span><br><span class="hljs-string">decode解码</span><br><span class="hljs-string">endswith判断是否以指定字符结尾</span><br><span class="hljs-string">startswith判定是否以指定字符开头</span><br><span class="hljs-string">isspace判断字符串是否全由空格组成</span><br><span class="hljs-string">ljust指定长度左对齐且不足处由指定字符补齐</span><br><span class="hljs-string">islower判断字符串是否全由小写字母组成</span><br><span class="hljs-string">isupper判断字符串是否全由大写字母组成</span><br><span class="hljs-string">rjust指定长度右对齐且不足处由指定字符补齐</span><br><span class="hljs-string">zfill指定长度右对齐且不足处由0补齐</span><br><span class="hljs-string">rsplit从右开始切割字符串</span><br><span class="hljs-string">swapcase大小写互换</span><br><span class="hljs-string">rpartition从目标字符串的末尾也就是右边开始搜索分割符</span><br><span class="hljs-string">partition从目标字符串的开始也搜索分割符</span><br><span class="hljs-string">chr将Unicode码转成str</span><br><span class="hljs-string">ord将str转换成Unicode</span><br><span class="hljs-string">title每个单词的首字母大写</span><br><span class="hljs-string">&#x27;&#x27;&#x27;</span><br>a = <span class="hljs-string">&#x27;qew reTy&#x27;</span><br>b = [<span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;b&#x27;</span>, <span class="hljs-string">&#x27;c&#x27;</span>]<br><span class="hljs-built_in">print</span>(a.center(<span class="hljs-number">30</span>, <span class="hljs-string">&#x27;-&#x27;</span>))<br><span class="hljs-built_in">print</span>(a.count(<span class="hljs-string">&#x27;e&#x27;</span>))<br><span class="hljs-built_in">print</span>(a.encode(<span class="hljs-string">&#x27;utf-8&#x27;</span>))<br><span class="hljs-built_in">print</span>(a.encode(<span class="hljs-string">&#x27;utf-8&#x27;</span>).decode(<span class="hljs-string">&#x27;utf-8&#x27;</span>))<br><span class="hljs-built_in">print</span>(a.endswith(<span class="hljs-string">&#x27;y&#x27;</span>))<br><span class="hljs-built_in">print</span>(a.startswith(<span class="hljs-string">&#x27;q&#x27;</span>))<br><span class="hljs-built_in">print</span>(a.isspace())<br><span class="hljs-built_in">print</span>(a.isupper())<br><span class="hljs-built_in">print</span>(a.ljust(<span class="hljs-number">30</span>, <span class="hljs-string">&#x27;-&#x27;</span>))<br><span class="hljs-built_in">print</span>(a.rjust(<span class="hljs-number">30</span>, <span class="hljs-string">&#x27;-&#x27;</span>))<br><span class="hljs-built_in">print</span>(a.zfill(<span class="hljs-number">30</span>))<br><span class="hljs-built_in">print</span>(a.rsplit())<br><span class="hljs-built_in">print</span>(a.swapcase())<br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;2020.2.2.demo1.py&#x27;</span>.rpartition(<span class="hljs-string">&#x27;.&#x27;</span>))<br><span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;2020.2.2.demo1.py&#x27;</span>.partition(<span class="hljs-string">&#x27;.&#x27;</span>))<br><span class="hljs-built_in">print</span>(a.<span class="hljs-built_in">chr</span>())<br><span class="hljs-built_in">print</span>(a.<span class="hljs-built_in">ord</span>())<br><span class="hljs-built_in">print</span>(a.title())<br></code></pre></td></tr></table></figure>

<h1 id="数字类型"><a href="#数字类型" class="headerlink" title="数字类型"></a>数字类型</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs python">num_212class=<span class="hljs-number">180</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">type</span>(num_212class))  <span class="hljs-comment"># 查看变量类型 </span><br><span class="hljs-built_in">print</span>(num_212class)<br>zx=<span class="hljs-number">9.3</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">type</span>(zx))<br><span class="hljs-built_in">print</span>(zx)<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">isinstance</span>(num_212class,<span class="hljs-built_in">int</span>))     <span class="hljs-comment"># 判断变量是否为某个数据类型，如果是指定某个类型则返回True,否则返回False</span><br><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">int</span>(<span class="hljs-string">&#x27;1&#x27;</span>))  <span class="hljs-comment"># 将长得和数字一样的字符串转化成数字</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">float</span>(<span class="hljs-string">&#x27;1.1&#x27;</span>))  <span class="hljs-comment"># 将长得和数字一样的字符串转化成数字</span><br></code></pre></td></tr></table></figure>

<h1 id="布尔"><a href="#布尔" class="headerlink" title="布尔"></a>布尔</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-built_in">print</span>(<span class="hljs-number">2</span> &gt; <span class="hljs-number">1</span>)  <span class="hljs-comment"># True</span><br><span class="hljs-built_in">print</span>(<span class="hljs-number">3</span> &lt; <span class="hljs-number">1</span>)  <span class="hljs-comment"># False</span><br><span class="hljs-built_in">print</span>(<span class="hljs-literal">True</span>)<br></code></pre></td></tr></table></figure>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-blog/categories/python/" class="category-chain-item">python</a>
  
  
    <span>></span>
    
  <a href="/my-blog/categories/python/Python%E5%9F%BA%E7%A1%80/" class="category-chain-item">Python基础</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/">#测试开发</a>
      
        <a href="/my-blog/tags/Python%E5%9F%BA%E7%A1%80/">#Python基础</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>python基础（上）</div>
      <div>https://walker_kwu.gitee.io/my-blog/2023/02/19/python/base/python基础（上）/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>木舟</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年2月19日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年5月23日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="NC - 非商业性使用">
                <i class="iconfont icon-nc"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="SA - 相同方式共享">
                <i class="iconfont icon-sa"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-blog/2023/02/19/python/base/python%E5%9F%BA%E7%A1%80%EF%BC%88%E4%B8%8B%EF%BC%89/" title="python基础（下）">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">python基础（下）</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-blog/2023/02/18/env/git/" title="git环境配置(windows)">
                        <span class="hidden-mobile">git环境配置(windows)</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>By 木舟</span> <i class="iconfont icon-love"></i> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-blog/js/events.js" ></script>
<script  src="/my-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
